﻿using MapleLeaf.Model.CookBook.Menu.Entity;

namespace MapleLeaf.WebApi.CookBook.Controllers.Dto {

    /// <summary>
    /// RouteVO，路由对象
    /// </summary>
    public class RouteVo {
        /// <summary>
        /// 子路由列表
        /// </summary>
        public List<RouteVo> Children { get; set; } = [];

        /// <summary>
        /// 组件路径
        /// </summary>
        public string Component { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Meta? Meta { get; set; }

        /// <summary>
        /// 路由名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 路由路径
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 跳转链接
        /// </summary>
        public string Redirect { get; set; }
    }

    /// <summary>
    /// Meta，路由属性类型
    /// </summary>
    public partial class Meta {
        /// <summary>
        /// 【目录】只有一个子路由是否始终显示
        /// </summary>
        public bool? AlwaysShow { get; set; }

        /// <summary>
        /// 是否隐藏(true-是 false-否)
        /// </summary>
        public bool? Hidden { get; set; }

        /// <summary>
        /// ICON
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 【菜单】是否开启页面缓存
        /// </summary>
        public bool? KeepAlive { get; set; }

        /// <summary>
        /// 路由参数
        /// </summary>
        public Dictionary<string, string>? Params { get; set; }

        /// <summary>
        /// 路由title
        /// </summary>
        public string Title { get; set; }
    }
    /// <summary>
    /// RouteExtensions，路由扩展
    /// </summary>
    public static class RouteExtensions {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static RouteVo ToVo(this MenuEntity entity) {
            var vo = new RouteVo {
                Name = entity.RoutePath ?? "",
                Path = entity.RoutePath ?? "",
                Component = entity.Component ?? "",
                Redirect = entity.Redirect ?? "",
                Meta = new Meta {
                    Title = entity.Name ?? "",
                    Icon = entity.Icon ?? "",
                    Hidden = entity.Visible == 0,
                    AlwaysShow = entity.AlwaysShow == 1,
                    KeepAlive = entity.KeepAlive == 1
                }
            };
            return vo;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static List<RouteVo> ToRouteVoList(this List<MenuEntity> entities) {
            var vos = new List<RouteVo>();
            var list = entities.Where(m => m.ParentId == null || m.ParentId == 0).OrderBy(m => m.Sort).ToList();
            foreach (var entity in list) {
                var vo = entity.ToVo();
                vo.Children = GetChildren(entities, entity.Id);
                vos.Add(vo);
            }
            return vos;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entities"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static List<RouteVo> GetChildren(this List<MenuEntity> entities, long parentId) {
            var children = entities.Where(m => m.ParentId == parentId).OrderBy(m => m.Sort).ToList();
            var vos = new List<RouteVo>();
            foreach (var child in children) {
                var vo = child.ToVo();
                vo.Children = GetChildren(entities, child.Id);
                vos.Add(vo);
            }
            return vos;
        }
    }
}
